package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.entity.ClueCardGroup;
import la.iok.hzsvn.lewin.movie.entity.ClueCardGroupInstance;
import la.iok.hzsvn.lewin.movie.mapper.ClueCardGroupInstanceMapper;
import la.iok.hzsvn.lewin.movie.model.ClueCardGroupVo;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.NotNullElement;
import la.iok.hzsvn.share.annotations.NullElement;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.number.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClueCardGroupInstanceServiceImpl extends BaseServiceImpl<ClueCardGroupInstance, ClueCardGroupInstanceMapper,Long> implements ClueCardGroupInstanceService {
    public ClueCardGroupInstanceServiceImpl(ClueCardGroupInstanceMapper mapper) {
        super(mapper);
    }

    @NotNull
    @Override
    public Long save(@NotNull ClueCardGroup group, @NotNull Long movieInstanceId) {
        ClueCardGroupInstance instance = new ClueCardGroupInstance();
        BeanUtils.copyProperties(group,instance);
        instance.setMovieInstanceId(movieInstanceId);
        instance.setId(null);
        instance.setClueCardGroupId(group.getId());
        insertSelective(instance);
        return instance.getId();
    }

    @NotNullElement
    @NotNull
    @Override
    public List<ClueCardGroupVo> clueCardGroups(@Nullable Long movieInstanceId) {
        List<ClueCardGroupInstance> list = listByProperty("movieInstanceId",movieInstanceId);
        return list.stream().map(this::po2vo).collect(Collectors.toList());
    }

    @Override
    public void markFileDeleted(@Nullable @NullElement Collection<Long> ids) {
        batchUpdate("fileDeleted",1, ids);
    }

    @NotNull
    private ClueCardGroupVo po2vo(@NotNull ClueCardGroupInstance po){
        ClueCardGroupVo vo = new ClueCardGroupVo();
        BeanUtils.copyProperties(po,vo);
        vo.setScenes(NumberUtils.toInts(po.getScenes(),ClueCardGroup.SCENE_SEQUENCE_SPLIT));
        vo.setId(po.getClueCardGroupId());
        return vo;
    }
}
